package com.papper.common.config;

import com.google.common.collect.Maps;
import com.papper.common.utils.tool.StringTool;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

public class LocalConfig {
    private final static Logger              logger             = LoggerFactory.getLogger(LocalConfig.class);
    private static       LocalConfig         instance           = null;
    private final static String              DEFAULT_NULL_VALUE = "NULL";
    private static final Map<String, String> Config             = Maps.newHashMap();

    public final static String COOKIE_DOMAIN = "cookie_domain";
    public final static String SYSTEM_CODE   = "system_code";
    public final static String SYSTEM_NAME   = "system_name";

    public static synchronized LocalConfig I() {
        if (instance == null) {
            instance = new LocalConfig();
            instance.initPropertys();
        }
        return instance;
    }

    public void initPropertys() {
        Config.clear();
        // 加载配置文件
        InputStream fis = Thread.currentThread().getContextClassLoader().getResourceAsStream("config.xml");
        if (fis == null) {
            logger.error("系统未找到配置文件:config.xml");
            return;
        }
        try {
            InputStreamReader isr      = new InputStreamReader(fis, StandardCharsets.UTF_8);
            SAXReader         reader   = new SAXReader();
            Document          document = reader.read(isr);
            List<Element>     lstPropS = document.getRootElement().elements("props");
            for (Element props : lstPropS) {
                List<Element> lstProp = props.elements("prop");
                for (Element prop : lstProp) {
                    Config.put(prop.attributeValue("key"), prop.getTextTrim());
                }
            }
        } catch (DocumentException e) {
            logger.error("读取配置文件异常:{}", e.getMessage());
        }
    }

    private String get(String key) {
        String rtn = Config.get(key);
        if (rtn != null) {
            if (DEFAULT_NULL_VALUE.equals(rtn)) {
                return null;
            }
            return rtn;
        }
        return rtn;
    }

    public String GS(String key, String def) {
        String rtn = get(key);
        return rtn == null ? def : rtn;
    }

    public String GS(String key) {
        String rtn = get(key);
        return rtn == null ? StringTool.EMPTY : rtn;
    }

    public int GI(String key, int def) {
        return StringTool.parseInteger(get(key), def);
    }

    public double GD(String key, double def) {
        return StringTool.parseDouble(get(key), def);
    }

    public boolean GB(String key) {
        return Boolean.parseBoolean(get(key));
    }
}
